/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rg1tcpdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/**
 *
 * @author Administrator
 */
public class SocketReadRunnable implements Runnable
{
    private Socket socket;
    private BufferedReader in;
    private JTextArea jTextArea1;

    public SocketReadRunnable(Socket socket, JTextArea jTextArea1)
    {
        this.socket = socket;
        this.jTextArea1 = jTextArea1;
        try
        {
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException ex)
        {
            Logger.getLogger(SocketReadRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run()
    {
        while(true)
        {
            
            try
            {
               String str=in.readLine();
               synchronized(jTextArea1)
               {
                   if(str!=null)
               jTextArea1.append(socket.getInetAddress().getHostName()+":"+str+"\n");
               }
            } catch (IOException ex)
            {
                Logger.getLogger(SocketReadRunnable.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
    
    
}
